import { TextareaItem } from "@ali/ding-ui";

import DatePicker from "../../components/DatePicker/index";
import InputItem from "../../components/InputItem/index";
// components
import Select from "../../components/Select";
// import AreaSelect from "../../components/AreaSelect";

import { type, getUrlParam } from "../../utils/common";
import { getUrlHashParam } from "../../utils/urlParams";

function isForeign() {
  return (
    [
      "ding86a9a9682604167035c2f4657eb6378f",
      "ding3f4e59195320857235c2f4657eb6378f"
    ].indexOf(getUrlParam("corpId")) !== -1
  );
}
const mapper = {
  TextField: InputItem,
  DDSelectField: Select,
  TextareaField: TextareaItem,
  DDDateField: DatePicker,
  // AreaSelect: InputItem,
  DDPhotoField: "DDPhotoField"
};

function isSystem(item) {
  return item.systemFlag || item.contactSystemFlag;
}

// 如果是HR的话url中会有userId
function isHR() {
  return !!getUrlHashParam("userId");
}
export default (items = []) => ({
  items: items
    .map(q => {
      const component = mapper[q.fieldType];

      const props = {
        value: q.value,
        options:
          type(q.optionText) === "string"
            ? JSON.parse(q.optionText || "{}")
            : undefined,
        fieldType: q.fieldType,
        id: q.fieldCode,
        label: q.fieldName,
        visible: q.visibleByEmp || isHR(),
        required:
          (q.required &&
            q.visibleByEmp &&
            q.editableByEmp &&
            !q.deleted &&
            !isHR()) ||
          q.fieldCode === "部门",
        editable: q.editableByEmp,
        placeholder: isForeign()
          ? component === DatePicker || component === Select
            ? "Please Select"
            : "Please Input"
          : undefined
      };
      if (component) {
        return {
          component,
          isSystem: isSystem(q),
          groupId: q.groupId,
          props,
          hint: q.hint
        };
      }
      return {
        component: "",
        props
      };
    })
    .filter(q => q) // 过滤掉空的（也就是说我目前不支持的）
});
